/*
 * ODO: Real-Time In-Memory ORM/DBMS Grid software
 * Copyright (c) 2008, Michael Oksenenko
 * All rights reserved.
 */
//----------------------------------------------------------------------------
#include "OsConfig.hpp"
#if ODO_OS_VISTA == ODO_CHOSEN_OS
//----------------------------------------------------------------------------
#include "OsComponentFactory.hpp"
#include "SyslogVista.hpp"
#include "Syslog.h"

using namespace odo;

void SyslogVista::log(const wchar_t* aMessage, WORD aMesageType) {
	if (isClosed()) {
		std::wcerr << L"FAIL report event on closed syslog: " << aMessage << std::endl;
		return;
	}
	if (0 == ReportEventW(
		mEventSource,	// handle to the event log
		aMesageType,	// type of event to be logged
		0,				// event category
		MSG_SYSLOG,		// event identifier (from Syslog.h)
		0,				// pointer to the current user's security identifier
		1,				// number of insert strings
		0,				// number of bytes of event raw data
		static_cast<LPCWSTR*>(&aMessage), // pointer to an array of null-strings
		0)				// pointer to the buffer containing the binary data
	) {
		OsComponentFactory::getError()->printLast();
		std::wcerr << L"FAIL report syslog event: " << aMessage << std::endl;
	}
}

void SyslogVista::open(const wchar_t* aSyslogName) {
	if (isOpen()) close();
	mEventSource = RegisterEventSourceW(0, static_cast<LPCWSTR>(aSyslogName));
	if (0 == mEventSource) {
		OsComponentFactory::getError()->printLast();
		std::wcerr << L"FAIL register syslog event source." << std::endl;
	}
}

void SyslogVista::close() {
	if (isClosed()) return;
	if (0 == DeregisterEventSource(mEventSource)) {
		OsComponentFactory::getError()->printLast();
		std::wcerr << L"FAIL deregister syslog event source." << std::endl;
	}
	mEventSource = 0;
}

bool SyslogVista::isOpen() {
	return (0 != mEventSource);
}

bool SyslogVista::isClosed() {
	return (0 == mEventSource);
}

void SyslogVista::logInfo(const wchar_t* aInfo) {
	log(aInfo, EVENTLOG_INFORMATION_TYPE);
	std::wcout << L"syslog_INFO: " << aInfo << std::endl;
}

void SyslogVista::logWarn(const wchar_t* aWarn) {
	log(aWarn, EVENTLOG_WARNING_TYPE);
	std::wcout << L"syslog_WARN: " << aWarn << std::endl;
}

void SyslogVista::logFail(const wchar_t* aFail) {
	log(aFail, EVENTLOG_ERROR_TYPE);
	std::wcerr << L"syslog_FAIL: " << aFail << std::endl;
}

SyslogVista::~SyslogVista() {
	close();
}

SyslogVista::SyslogVista() {
	mEventSource = 0;
}
//----------------------------------------------------------------------------
#endif /* ODO_OS_VISTA */
//----------------------------------------------------------------------------
